/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.CustomerManager;

import BLL.Customer_BLL;
import DTO.Customer_DTO;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Mr.Sam
 */
public class CustomerManager extends javax.swing.JInternalFrame {

    TableCustomer model = new TableCustomer();
    String sql = "select * from customer";
    Customer_BLL customer_bll = new Customer_BLL();

    public CustomerManager() throws Exception {
        initComponents();
        MaKH.setEditable(false);
        model.SetTable(table);
        model.SetData(sql);
        CMND.setDocument(getPlainDocument(11));
        SoDT.setDocument(getPlainDocument(11));
    }

    public static PlainDocument getPlainDocument(final int maxLength) {
        PlainDocument p = new PlainDocument() {
            @Override
            public void insertString(int arg0, String arg1, AttributeSet arg2)
                    throws BadLocationException {
// TODO Auto-generated method stub
                if (getLength() + arg1.length() <= maxLength) {
                    super.insertString(arg0, arg1, arg2);
                }
            }
        };
        return p;
    }

    public void ReLoadTable(String sql) {
        model.RemoveData();
        try {
            model.SetData(sql);
        } catch (Exception ex) {
            Logger.getLogger(CustomerManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jLabel1 = new javax.swing.JLabel();
        HoTen = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        Email = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        SoDT = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        MaKH = new javax.swing.JTextField();
        DiaChi = new javax.swing.JTextField();
        CMND = new javax.swing.JTextField();
        Sua = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        Them = new javax.swing.JButton();
        Xoa = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        setTitle("Quản Lý Thông Tin Khách Hàng");

        jXPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel1.setText("Mã KH");
        jXPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(38, 11, -1, 28));

        HoTen.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                HoTenKeyTyped(evt);
            }
        });
        jXPanel1.add(HoTen, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 50, 161, 28));

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel2.setText("Họ Tên");
        jXPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(38, 50, -1, 28));

        Email.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                EmailKeyTyped(evt);
            }
        });
        jXPanel1.add(Email, new org.netbeans.lib.awtextra.AbsoluteConstraints(141, 89, 161, 28));

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel3.setText("Email");
        jXPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(38, 89, -1, 28));

        SoDT.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                SoDTKeyTyped(evt);
            }
        });
        jXPanel1.add(SoDT, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 90, 160, 28));

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel4.setText("Địa Chỉ");
        jXPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(331, 11, -1, 28));

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel5.setText("CMND");
        jXPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(331, 50, -1, 28));

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel6.setText("Số ĐT");
        jXPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(331, 93, -1, -1));
        jXPanel1.add(MaKH, new org.netbeans.lib.awtextra.AbsoluteConstraints(141, 11, 161, 28));
        jXPanel1.add(DiaChi, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 10, 160, 28));

        CMND.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                CMNDKeyTyped(evt);
            }
        });
        jXPanel1.add(CMND, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 50, 160, 28));

        Sua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Small_Connect.png"))); // NOI18N
        Sua.setText("Sửa");
        Sua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SuaActionPerformed(evt);
            }
        });
        jXPanel1.add(Sua, new org.netbeans.lib.awtextra.AbsoluteConstraints(810, 70, 180, 40));

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Small_Update.png"))); // NOI18N
        jButton2.setText("Nhập lại");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jXPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(580, 70, 190, 40));

        Them.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Small_AddCustomer.png"))); // NOI18N
        Them.setText("Thêm ");
        Them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ThemActionPerformed(evt);
            }
        });
        jXPanel1.add(Them, new org.netbeans.lib.awtextra.AbsoluteConstraints(580, 20, 190, 40));

        Xoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Small_Close.png"))); // NOI18N
        Xoa.setText("Xóa");
        Xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                XoaActionPerformed(evt);
            }
        });
        jXPanel1.add(Xoa, new org.netbeans.lib.awtextra.AbsoluteConstraints(810, 20, 180, 40));

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(table);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1001, Short.MAX_VALUE)
                    .addComponent(jXPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(18, Short.MAX_VALUE)
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
        // TODO add your handling code here:
        int index = table.getSelectedRow();
        MaKH.setText(table.getValueAt(index, 0) + "");
        HoTen.setText(table.getValueAt(index, 1) + "");
        CMND.setText(table.getValueAt(index, 2) + "");
        DiaChi.setText(table.getValueAt(index, 3) + "");
        Email.setText(table.getValueAt(index, 4) + "");
        SoDT.setText(table.getValueAt(index, 5) + "");

    }//GEN-LAST:event_tableMouseClicked
    public void ClearAll() {
        MaKH.setText("");
        HoTen.setText("");
        CMND.setText("");
        DiaChi.setText("");
        Email.setText("");
        SoDT.setText("");
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        ClearAll();
    }//GEN-LAST:event_jButton2ActionPerformed
    public Customer_DTO Get_Info_Customer() {
        Customer_DTO info = new Customer_DTO();
        info.setCustomerID(MaKH.getText().trim());
        info.setFullName(HoTen.getText().trim());
        info.setIDNumber(CMND.getText().trim());
        info.setHomeAddress(DiaChi.getText().trim());
        info.setEmail(Email.getText().trim());
        info.setPhone(SoDT.getText().trim());
        return info;
    }
    private void ThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ThemActionPerformed
        // TODO add your handling code here:

        try {
            customer_bll.Insert_Update("{call dbo.Insert_Customer(?,?,?,?,?,?)}", Get_Info_Customer());
            JOptionPane.showMessageDialog(null, "Them Thanh Cong");
            ReLoadTable(sql);
        } catch (Exception e) {
        }

    }//GEN-LAST:event_ThemActionPerformed

    private void XoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_XoaActionPerformed
        // TODO add your handling code here:
        try {
            if (customer_bll.Delete("{call dbo.Delete_Customer(?,?)}", MaKH.getText().trim()) == true) {
                JOptionPane.showMessageDialog(null, "co the xoa");
                JOptionPane.showMessageDialog(null, "Xoa Thanh Cong");
                ClearAll();
                ReLoadTable(sql);
            } else {
                JOptionPane.showMessageDialog(null, "khong the xoa");
            }

        } catch (Exception e) {
        }

    }//GEN-LAST:event_XoaActionPerformed

    private void SuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SuaActionPerformed
        // TODO add your handling code here:
        try {
            customer_bll.Insert_Update("{call dbo.Update_Customer(?,?,?,?,?,?)}", Get_Info_Customer());
            JOptionPane.showMessageDialog(null, "Sua Thanh Cong");
            ReLoadTable(sql);
        } catch (Exception e) {
        }
    }//GEN-LAST:event_SuaActionPerformed

    private void CMNDKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_CMNDKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if ((c < 48 || c > 57)) {
            evt.consume();
        }


    }//GEN-LAST:event_CMNDKeyTyped

    private void HoTenKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_HoTenKeyTyped
        char c = evt.getKeyChar();
        if ((c < 32) || (c > 32 && c < 65) || (90 < c && c < 97) || c > 122) {
            evt.consume();
        }
    }//GEN-LAST:event_HoTenKeyTyped

    private void SoDTKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_SoDTKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if ((c < 48 || c > 57)) {
            evt.consume();
        }

    }//GEN-LAST:event_SoDTKeyTyped

    private void EmailKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_EmailKeyTyped
        // TODO add your handling code here:
        char c=evt.getKeyChar();
        if(c<46||c==47||(c>57&&c<64)||(c>90&&c<97)||c>122)
        {
            evt.consume();
        }
    }//GEN-LAST:event_EmailKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField CMND;
    private javax.swing.JTextField DiaChi;
    private javax.swing.JTextField Email;
    private javax.swing.JTextField HoTen;
    private javax.swing.JTextField MaKH;
    private javax.swing.JTextField SoDT;
    private javax.swing.JButton Sua;
    private javax.swing.JButton Them;
    private javax.swing.JButton Xoa;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
}
